package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.dto.LecStatisticsOrgDTO;
import com.cheer.entity.LecCourseEntity;
import com.cheer.param.LecCourseListParam;
import com.cheer.param.LecTeacherListParam;
import com.cheer.vo.LecCourseVO;
import com.cheer.vo.LecTeacherVO;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 用户信息
 *
 * @author cheer
 */
@Mapper
@Repository
public interface LecCourseDao extends BaseMapper<LecCourseEntity> {

    /**
     * 教师过滤条件查询
     * @param lecTeacherListParam
     * @return
     */
    List<LecCourseEntity> listByTea(@Param("lecTeacherListParam") LecTeacherListParam lecTeacherListParam);

    /**
     * 课程过滤条件查询
     * @param lecCourseListParam
     * @return
     */
    IPage<LecCourseVO> listByCoursePage( @Param("page") Page<LecCourseVO> page, @Param("lecCourseListParam") LecCourseListParam lecCourseListParam );

    /**
     * 课程过滤条件查询
     * @param lecCourseListParam
     * @return
     */
    List<LecCourseEntity> listByCourse(@Param("lecCourseListParam") LecCourseListParam lecCourseListParam );


    default Integer selectCountByTeacher(Integer teacherId) {
        return selectCount(new QueryWrapper<LecCourseEntity>()
                .eq("teacher_id", teacherId)
                .eq("status", 2));
    }

    default IPage<LecCourseEntity> selectPageByQueryCondition(Integer pageNum, Integer pageSize, String keyword, Integer status, String path) {
        QueryWrapper<LecCourseEntity> queryWrapper = new QueryWrapper<>();

        String trimName = StringUtils.trimToNull(keyword);
        if (trimName != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .like("title", trimName));
        }

        if (status != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .eq("status", status));
        } else {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .in("status", Lists.newArrayList(1, 2, 3)));
        }

        if (path != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .likeRight("path", path));
        }

        return selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }

    default List<LecCourseEntity> selectPassedCourse(String keyword, String path) {
        QueryWrapper<LecCourseEntity> queryWrapper = new QueryWrapper<>();

        String trimName = StringUtils.trimToNull(keyword);
        if (trimName != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .like("title", trimName));
        }

        queryWrapper.eq("status", 2);

        if (path != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .likeRight("path", path));
        }

        return selectList(queryWrapper);
    }

    default IPage<LecCourseEntity> selectPageByTeacherId(Integer pageNum, Integer pageSize, String keyword, Integer status, Integer id) {
        QueryWrapper<LecCourseEntity> queryWrapper = new QueryWrapper<>();

        String trimName = StringUtils.trimToNull(keyword);
        if (trimName != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .like("title", trimName));
        }

        if (status != null) {
            queryWrapper.and(lecTeacherEntityQueryWrapper -> lecTeacherEntityQueryWrapper
                    .eq("status", status));
        }

        queryWrapper.eq("teacher_id", id);

        return selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }


    default int countByStatus(Integer status, String path){

        QueryWrapper<LecCourseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LecCourseEntity::getStatus, status).likeRight(LecCourseEntity::getPath, path);
        return selectCount(queryWrapper);
    }

    Integer getStatisticsOrg(@Param("year") String year, @Param("path") String path);


    /**
     * 根据Path 获取
     * @param path
     * @return
     */
    default int countByPath(String path){
        QueryWrapper<LecCourseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().likeRight(LecCourseEntity::getPath, path);
        return selectCount(queryWrapper);
    }
}
